package com.asm.slidingmenu;

import java.security.KeyStore.LoadStoreParameter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.asm.DBloader.ASM_AVTempDS;
import com.asm.DBloader.ASM_FAvDet;
import com.asm.DBloader.ASM_FAvDetDS;
import com.asm.DBloader.ASM_FAvHedDS;
import com.asm.DBloader.ASM_FCompanyBranch;
import com.asm.DBloader.ASM_FCompanyBranchDS;
import com.asm.DBloader.ASM_FDayExpDet;
import com.asm.DBloader.ASM_FDayExpDetDS;
import com.asm.DBloader.ASM_FDayExpHed;
import com.asm.DBloader.ASM_FDayExpHedDS;
import com.asm.DBloader.ASM_FDaynPrdHed;
import com.asm.DBloader.ASM_FDaynPrdHedDS;
import com.asm.DBloader.ASM_FExpTemp;
import com.asm.DBloader.ASM_FExpTempDS;
import com.asm.DBloader.ASM_FdebtorDS;
import com.asm.DBloader.ASM_UsersettingsDS;
import com.asm.DBloader.ASM_fCompanySettingDS;
import com.asm.DBloader.ASM_fDaynPrdDetDS;
import com.asm.DBloader.ASM_fExpenseDS;
import com.asm.DBloader.ASM_fReasonDS;
import com.dspl.asmswd.R;

import android.R.string;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.ListView;


public class ASM_Expences_fragment extends Fragment {
	
	public ASM_Expences_fragment(){}
	private ViewGroup mContainerView,mContainerView1;
	String selectedReason = null;
	String getUpdateExpencesNo = null;

	String ASMCode =null;

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		
        View rootView = inflater.inflate(R.layout.activity_layout_changes, container, false);
        setHasOptionsMenu(true);
        mContainerView = (ViewGroup)rootView.findViewById(R.id.container);
        loadhistory();
        
       return rootView;
    }
	
	
	
	
	public void addwindow(String status,final String refno) 
	{
	
		
		if(status.equals("UPDAET"))
		{

		 	final Dialog dialog = new Dialog(getActivity());
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(R.layout.asm_fragment_expences);
			dialog.setTitle("EXPENCES");
			dialog.setCancelable(false);
			dialog.show();

			Button btncancel = (Button)dialog.findViewById(R.id.btncancel);
			Button btnclear = (Button)dialog.findViewById(R.id.btnclear);
			final Button btnadd = (Button)dialog.findViewById(R.id.Button01);
			
			Button btnsave = (Button)dialog.findViewById(R.id.btnsave);
			final EditText txtremark = (EditText)dialog.findViewById(R.id.txtremarks);
			final EditText txtamt = (EditText)dialog.findViewById(R.id.txtamt);
			final EditText txtdate = (EditText)dialog.findViewById(R.id.txtdate);
			final EditText txtrefno = (EditText)dialog.findViewById(R.id.txtrefno);
			final Spinner spinner = (Spinner)dialog.findViewById(R.id.spin1E);
			
//			txtamt.setText("");
			//txtremark.setText(" ");
			
			
			//txtdate.setText(getCurrentTimeString().toString());
		

			 final TableLayout stk = (TableLayout)dialog.findViewById(R.id.table_main);
			 final TableRow tbrow01 = new TableRow(getActivity());
		     tbrow01.setGravity(Gravity.RIGHT);
			
		     ASM_FDayExpDetDS ds3=new ASM_FDayExpDetDS(getActivity());
		     final ArrayList<String> lstdet =ds3.getfDayExpDetfull(refno);
		     
		     ASM_FDayExpHedDS ds4=new ASM_FDayExpHedDS(getActivity());
		     final ArrayList<String> lstHed =ds4.getExpHedforLoad(refno);
		     final ArrayList<String> lstHedremarks =ds4.getExpHedforLoadRemarks(refno);
		     
		     txtdate.setText(lstHed.get(0).toString());
		     txtremark.setText(lstHedremarks.get(0).toString());
		     
		     
		     
		     

		     for (int i = 0; i < lstdet.size(); i++) 
		     {
		    	 final Button tv00 = new Button(getActivity());
		            tv00.setText(lstdet.get(i).split("-:-")[1].toString()+"-:-"+lstdet.get(i).split("-:-")[2].toString()+"-:-"+lstdet.get(i).split("-:-")[3].toString());
		            tv00.setClickable(true);
		            tv00.setWidth(600);
		            tv00.setTextSize(19);
		            tv00.setTextColor(Color.BLACK);
		            tv00.setGravity(Gravity.LEFT);
		            stk.addView(tv00);
		     

		txtrefno.setText(refno);
		
		tv00.setOnClickListener(new View.OnClickListener() 
		{
						
			public void onClick(View v) 
			{
				AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
				builder1.setTitle("ATTENTION..!");
				builder1.setIcon(R.drawable.ic_attention);
	            builder1.setMessage("You Can't Edit this Item..If you want to Modify,Please Delete First and add as a New Record..");
	            builder1.setCancelable(true);
	            builder1.setPositiveButton("Ok",
	                new DialogInterface.OnClickListener()
	            {
	                public void onClick(DialogInterface dg, int id) 
	                {
	                		//tbrow01.removeViewsInLayout(1, 1); 
	                	dg.dismiss();
	               
	                }
	            });
	            AlertDialog alert11 = builder1.create();
	            alert11.show();
//    
//				     
					}
			
				});
//		
		
		tv00.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
				builder1.setTitle("ATTENTION..!");
				builder1.setIcon(R.drawable.ic_attention);
	            builder1.setMessage("Are You Sure Want to Delete this Record...?");
	            builder1.setCancelable(true);
	            builder1.setPositiveButton("YES",
	                new DialogInterface.OnClickListener()
	            {
	                public void onClick(DialogInterface dg, int id) 
	                {
	                		//tbrow01.removeViewsInLayout(1, 1); 
	                	try
	                	{

	                	   ASM_FDayExpDetDS ds2=new ASM_FDayExpDetDS(getActivity());
	                  	   ds2.Delete(txtrefno.getText().toString().trim(),tv00.getText().toString().split("-:-")[1]);
	                	   tv00.setText("DELETED");
	                	   tv00.setTextColor(Color.RED);
	                		
	                	}catch(Exception e)
	                	{
	                		
	                	}
           
	                }
	            });
	            builder1.setNegativeButton("NO", new DialogInterface.OnClickListener() 
	            {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
	            AlertDialog alert11 = builder1.create();
	            alert11.show();	
				return false;
				
				
			}
		});
        
	     } 
        
		     
		     
		ASM_fExpenseDS ds2=new ASM_fExpenseDS(getActivity());
		ArrayList<String> list =ds2.getExpense();
		list.add(0,"SELECT EXPENCES");
		final ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
		spinner.setAdapter(categoriesAdapter);
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
		{

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) 
			{
				// TODO Auto-generated method stub
				selectedReason = parent.getItemAtPosition(position).toString();
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) 
			{
				// TODO Auto-generated method stub
				
			}
		});
		
		
		
		btnadd.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				 final TableRow tbrow0 = new TableRow(getActivity());
			      tbrow0.setGravity(Gravity.RIGHT);
			      
//*********Updatate newly added Record********//	
			      
			      if(btnadd.getText().toString().equals("UPDATEO"))//ok
			      {
			    	

			    		try {

							
						  	ASM_FDayExpDet asDblist4 = new ASM_FDayExpDet();
							ASM_FDayExpDetDS DS5 = new ASM_FDayExpDetDS(getActivity());

							asDblist4.setAsmDExpDet_ExpCode(selectedReason.toString().split("-:-")[1]);
							asDblist4.setAsmDExpDet_ExpName(selectedReason.split("-:-")[0].toString());
							asDblist4.setAsmDExpDet_Amt(txtamt.getText().toString());
							asDblist4.setAsmDExpDet_RefNo(txtrefno.getText().toString());
							
							DS5.UpdateIN(asDblist4);
							btnadd.setText("ADD");
							
							final Button tv0 = new Button(getActivity());
					        tv0.setText(selectedReason+"-:-"+txtamt.getText().toString());
					        tv0.setClickable(true);
					        tv0.setWidth(600);
					        tv0.setTextSize(19);
					        tv0.setTextColor(Color.RED);
					        tv0.setGravity(Gravity.LEFT);
					        tbrow0.addView(tv0);
					        stk.addView(tbrow0);
					        btnadd.setText("ADD");
					        
					        spinner.setAdapter(categoriesAdapter);
					        spinner.setClickable(true);
							
							Toast.makeText(getActivity(), "Record Update Sucessful...", 
				                  	   Toast.LENGTH_LONG).show();
							
							
							tv0.setOnClickListener(new View.OnClickListener() 
							{
								
								
								public void onClick(View v) 
								{
//									// TODO Auto-generated method stub
								tv0.setClickable(false);
								txtamt.setText(tv0.getText().toString().split("-:-")[2]);
								stk.removeViews(0, 1);
							
								
								ASM_FDayExpDetDS dst=new ASM_FDayExpDetDS(getActivity());
								final ArrayList<String> lstdetn =dst.getfDayExpdetforUpdate(tv0.getText().toString().split("-:-")[1],refno);

								ArrayList<String> list1 =lstdet;
								list1.add(0,tv0.getText().toString().split("-:-")[0]+"-:-"+tv0.getText().toString().split("-:-")[1]);
							
								ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
								spinner.setAdapter(categoriesAdapter);
								spinner.setClickable(false);
								
								
								
								btnadd.setText("UPDATEO");
									     
									     
								}
								
									});
							
							
							
							}
			    		catch (Exception e)
							{
								// TODO: handle exception
								Toast.makeText(getActivity(), "Record Update Not Sucessful...Try Again..", 
				                  	   Toast.LENGTH_LONG).show();
							} 
			    	 
			      
			      }  
			      
			      else if(btnadd.getText().toString().equals("UPDATE"))
			      {
					ASM_FExpTempDS ds3=new ASM_FExpTempDS(getActivity());
					ArrayList<String> list1 =ds3.getExpTemp1(selectedReason.split("-:-")[1].toString());
					
				
				
					try {
				
				
			  	ASM_FExpTemp asDblist4 = new ASM_FExpTemp();
				ASM_FExpTempDS DS5 = new ASM_FExpTempDS(getActivity());

				
				asDblist4.setAsmExpTEmp_RefNo(txtrefno.getText().toString());
				asDblist4.setAsmExpTEmp_ExpName(selectedReason.split("-:-")[0].toString());
				asDblist4.setAsmExpTEmp_ExpCode(selectedReason.split("-:-")[1].toString());
				asDblist4.setAsmExpTEmp_amunt(txtamt.getText().toString());
				
				DS5.Updatetemp(asDblist4);
				
				} catch (Exception e) 
				{
					Toast.makeText(getActivity(), "Record Update Not Sucessful...Try Again..", 
                         	   Toast.LENGTH_LONG).show();
					// TODO: handle exception
				}
	
	//*********Refresh newly edited UI**************//
					final Button tv0 = new Button(getActivity());
			        tv0.setText(selectedReason+"-:-"+txtamt.getText().toString());
			        tv0.setClickable(true);
			        tv0.setWidth(600);
			        tv0.setTextSize(19);
			        tv0.setTextColor(Color.BLUE);
			        tv0.setGravity(Gravity.LEFT);
			        tbrow0.addView(tv0);
			        stk.addView(tbrow0);
			        btnadd.setText("ADD");
			        spinner.setClickable(true);
			      //  txtamt.setText("");
			        
	//************Update edited Record**************//		        
			   	 tv0.setOnClickListener(new View.OnClickListener() 
			        {
						
						@Override
						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
							if(btnadd.getText().toString().equals("UPDATE"))
							{
								tv0.setClickable(false);
							}else 
							{
			
							spinner.clearAnimation();
							tbrow0.removeViews(0, 1);
							
							
							
							ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
							ArrayList<String> list =ds2.getExpTemp1(tv0.getText().toString().split("-:-")[1]);
							
							ASM_fExpenseDS ds3=new ASM_fExpenseDS(getActivity());
							ArrayList<String> list1 =ds3.getExpense();
							


							
							list1.add(0,list.get(0).split("-:-")[1].toString()+"-:-"+list.get(0).split("-:-")[2].toString());
							
							ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
							spinner.setAdapter(categoriesAdapter);
							
							txtamt.setText(list.get(0).split("-:-")[3].toString());
							tv0.setClickable(false);
							spinner.setClickable(false);
							btnadd.setText("UPDATE");
							}
						}
					});
//***********Delete Udated record************************//			   	 
				 tv0.setOnLongClickListener(new View.OnLongClickListener() 
				 {
					
					@Override
					public boolean onLongClick(View v) 
					{
						// TODO Auto-generated method stub
						AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
						builder1.setTitle("ATTENTION..!");
						builder1.setIcon(R.drawable.ic_attention);
			            builder1.setMessage("Are You Sure Want to Delete this Record...?");
			            builder1.setCancelable(true);
			            builder1.setPositiveButton("YES",
			                new DialogInterface.OnClickListener()
			            {
			                public void onClick(DialogInterface dg, int id) 
			                {
			                	tbrow0.removeViews(0,1);
								spinner.clearAnimation();
			                	   ASM_FDayExpDetDS ds2=new ASM_FDayExpDetDS(getActivity());
			                	   ds2.Delete(txtrefno.getText().toString().trim(),selectedReason.split("-:-")[1].toString().trim());
		           
			                }
			            });
			            builder1.setNegativeButton("NO", new DialogInterface.OnClickListener() 
			            {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								dialog.dismiss();
							}
						});
			            AlertDialog alert11 = builder1.create();
			            alert11.show();	
						return false;
					}
				});
				//}
					
				}
//*******************Add new expences*********************************//				
				else if (btnadd.getText().toString().equals("ADD")) 
				{
					if(!selectedReason.toString().equals("SELECT EXPENCES") && !txtamt.getText().toString().equals(""))
					{
						ASM_FExpTempDS ds3=new ASM_FExpTempDS(getActivity());
						ArrayList<String> list1 =ds3.getExpTemp1(selectedReason.split("-:-")[1].toString());//[]

					   	ASM_FDayExpDetDS dss2=new ASM_FDayExpDetDS(getActivity());
			    	  	String list3 =dss2.getsaveddet(txtrefno.getText().toString(),selectedReason.toString().split("-:-")[1]);//null
			    	  	
//			    		ASM_FExpTempDS dss1=new ASM_FExpTempDS(getActivity());
//						String list2 =dss1.getsaveddet(txtrefno.getText().toString(),selectedReason.toString().split("-:-")[1]);
			    
			    	  
//			    	  if(list2!=null && list3!=null)
//			    	  {
//			    		  
//			    	  }
						
						
						if(!list1.toString().equals("[]") && list1.get(0).toString().split("-:-")[2].equals(selectedReason.split("-:-")[1].toString())||list3!=null)
						{

							Error1();
							

						}
						else 
						{

				        
				        try 
				        {
				        final Button tv0 = new Button(getActivity());
				        tv0.setText(selectedReason+"-:-"+txtamt.getText().toString());
				        tv0.setClickable(true);
				        tv0.setWidth(600);
				        tv0.setTextSize(19);
				        tv0.setTextColor(Color.BLUE);
				        tv0.setGravity(Gravity.LEFT);
				        tbrow0.addView(tv0);
				        stk.addView(tbrow0);

				        
				    	ASM_FExpTemp asDblist4 = new ASM_FExpTemp();
						ASM_FExpTempDS DS5 = new ASM_FExpTempDS(getActivity());
						
						
						asDblist4.setAsmExpTEmp_RefNo(txtrefno.getText().toString());
						
						asDblist4.setAsmExpTEmp_ExpName(selectedReason.split("-:-")[0].toString());
						asDblist4.setAsmExpTEmp_ExpCode(selectedReason.split("-:-")[1].toString());
						asDblist4.setAsmExpTEmp_amunt(txtamt.getText().toString());

						DS5.Create(asDblist4);

//*******************Update New Added expences*********************************//						
						 tv0.setOnClickListener(new View.OnClickListener() 
					        {
								
								@Override
								public void onClick(View v) 
								{
									// TODO Auto-generated method stub
									if(btnadd.getText().toString().equals("UPDATE"))
									{
										tv0.setClickable(false);
									}else 
									{
					
									tbrow0.removeViews(0, 1);
									spinner.clearAnimation();
									getUpdateExpencesNo = tv0.getText().toString().trim();
									
									ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
									ArrayList<String> list =ds2.getExpTemp1(tv0.getText().toString().split("-:-")[1]);
									
									ASM_fExpenseDS ds3=new ASM_fExpenseDS(getActivity());
									ArrayList<String> list1 =ds3.getExpense();
									
									list1.add(0,list.get(0).split("-:-")[1].toString()+"-:-"+list.get(0).split("-:-")[2].toString());
									
									ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
									spinner.setAdapter(categoriesAdapter);
									spinner.setClickable(false);
									txtamt.setText(list.get(0).split("-:-")[3].toString());
									tv0.setClickable(false);
									btnadd.setText("UPDATE");
									}
									
								}
							});

//*******************Delete newly added expences*********************************//						 
						 tv0.setOnLongClickListener(new View.OnLongClickListener() 
						 {
							
							@Override
							public boolean onLongClick(View v) 
							{
								// TODO Auto-generated method stub
								AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
								builder1.setTitle("ATTENTION..!");
								builder1.setIcon(R.drawable.ic_attention);
					            builder1.setMessage("Are You Sure Want to Delete this Record...?");
					            builder1.setCancelable(true);
					            builder1.setPositiveButton("YES",
					                new DialogInterface.OnClickListener()
					            {
					                public void onClick(DialogInterface dg, int id) 
					                {
					                	tbrow0.removeViews(0,1);
										spinner.clearAnimation();
										   ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());

					                	   ds2.Delete(tv0.getText().toString().split("-:-")[1].trim());
					           
					                }
					            });
					            builder1.setNegativeButton("NO", new DialogInterface.OnClickListener() 
					            {
									
									@Override
									public void onClick(DialogInterface dialog, int which) {
										// TODO Auto-generated method stub
										dialog.dismiss();
									}
								});
					            AlertDialog alert11 = builder1.create();
					            alert11.show();	
								return false;
							}
						});

				    	} catch (Exception e) 
				    	{
				    		Toast.makeText(getActivity(), "Record Save Not Sucessful...Try Again..", 
	                         	   Toast.LENGTH_LONG).show();
							// TODO: handle exception
						}

					}
					}
					else
					{
						Error();

					}
				}
				else 
				{
					
				}
				
			}
		});
		
		btnsave.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
				ASM_FExpTempDS ds7=new ASM_FExpTempDS(getActivity());
				ArrayList<String> list7 =ds7.getExpTemp();	
				if(!list7.toString().equals("[]"))
				{
				try 
				{

					
				ASM_FDayExpHed asDblist6 = new ASM_FDayExpHed();
				ASM_FDayExpHedDS DS6 = new ASM_FDayExpHedDS(getActivity());
				
				ASM_FDayExpDet asDblist7 = new ASM_FDayExpDet();
				ASM_FDayExpDetDS DS7 = new ASM_FDayExpDetDS(getActivity());
				
				for (int i = 0; i < list7.size(); i++) 
				{
					
					
					asDblist7.setAsmDExpDet_RefNo(list7.get(i).split("-:-")[0].toString());
					asDblist7.setAsmDExpDet_ExpCode(list7.get(i).split("-:-")[1].toString());
					asDblist7.setAsmDExpDet_ExpName(list7.get(i).split("-:-")[2].toString());
					asDblist7.setAsmDExpDet_Amt(list7.get(i).split("-:-")[3].toString());
					DS7.Create(asDblist7);
					
					
				}
				
				asDblist6.setAsmDExpHed_RefNo(txtrefno.getText().toString());
				asDblist6.setAsmDExpHed_TxnDate(txtdate.getText().toString());
				asDblist6.setAsmDExpHed_Remarks(txtremark.getText().toString());
				asDblist6.setAsmDExpHed_AddDate(txtdate.getText().toString());
				asDblist6.setAsmDExpHed_AddUser("");
				DS6.Update(asDblist6);
				
				
				ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
          	    ds2.Delete1(txtrefno.getText().toString().trim());
				
          	  //addItem(txtrefno.getText().toString(), txtdate.getText().toString());
          	  loadhistory();
				
				} catch (Exception e) 
				{
					Toast.makeText(getActivity(), "Record Save Not Sucessful...Try Again..", 
                      	   Toast.LENGTH_LONG).show();
					// TODO: handle exception
				}
				
				
		
				dialog.dismiss();
				
				}else
				{
					Error2();
				}
			}
		});
		
		btncancel.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				final ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
				String chk = ds2.getEmptyOrnot(txtrefno.getText().toString());
				if(chk!=null)
				{
			
				// TODO Auto-generated method stub
				AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
				builder1.setTitle("ATTENTION..!");
				builder1.setIcon(R.drawable.ic_attention);
	            builder1.setMessage("Are You Sure Want to Exit without saving...?");
	            builder1.setCancelable(true);
	            builder1.setPositiveButton("YES",
	                new DialogInterface.OnClickListener()
	            {
	                public void onClick(DialogInterface dg, int id) 
	                {
	                	  
	                  	  ds2.Delete1(txtrefno.getText().toString());
	                	  dialog.dismiss();
	                	  loadhistory();
	                	  
	           
	                }
	            });
	            builder1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						//dg.dismiss();
						
	                	   dialog.dismiss();
	                	   //loadhistory();
	                
					}
				});
	            AlertDialog alert11 = builder1.create();
	            alert11.show();	
				//dialog.dismiss();
				}else {
					 dialog.dismiss();
					 loadhistory();
				}
				
			}
		});
			
			
		}
		
		else if(status=="ADDNEW") 
		{
			final Dialog dialog = new Dialog(getActivity());
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(R.layout.asm_fragment_expences);
			dialog.setTitle("EXPENCES");
			dialog.setCancelable(false);
			final TableLayout stk = (TableLayout)dialog.findViewById(R.id.table_main);
			
			Button btncancel = (Button)dialog.findViewById(R.id.btncancel);
			Button btnclear = (Button)dialog.findViewById(R.id.btnclear);
			final Button btnadd = (Button)dialog.findViewById(R.id.Button01);
			
			Button btnsave = (Button)dialog.findViewById(R.id.btnsave);
			final EditText txtremark = (EditText)dialog.findViewById(R.id.txtremarks);
			final EditText txtamt = (EditText)dialog.findViewById(R.id.txtamt);
			final EditText txtdate = (EditText)dialog.findViewById(R.id.txtdate);
			final EditText txtrefno = (EditText)dialog.findViewById(R.id.txtrefno);
			final Spinner spinner = (Spinner)dialog.findViewById(R.id.spin1E);
			txtdate.setText(getCurrentTimeString().toString());
			dialog.show();
	
			//txtremark.setText(" ");
			
			
			
			
			final String cSettingcode = "ASMEXPENSE";
			String empty = "empty";
			
		
	
		final String Refno = refnogenrator(cSettingcode,empty);
		txtrefno.setText(Refno);
		
		
		
		
		ASM_fExpenseDS ds2=new ASM_fExpenseDS(getActivity());
		ArrayList<String> list =ds2.getExpense();
		list.add(0,"SELECT EXPENCES");
		ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
		spinner.setAdapter(categoriesAdapter);
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
		{

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				selectedReason = parent.getItemAtPosition(position).toString();
				//txtamt.setText("");
				
			
				
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		
		btnadd.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				 final TableRow tbrow0 = new TableRow(getActivity());
			      tbrow0.setGravity(Gravity.RIGHT);
			      
//*********Updatate newly added Record********//			      
				if(btnadd.getText().toString().equals("UPDATE"))
				{
					ASM_FExpTempDS ds3=new ASM_FExpTempDS(getActivity());
					ArrayList<String> list1 =ds3.getExpTemp1(selectedReason.split("-:-")[1].toString());
	
				
					try {
				
				
			  	ASM_FExpTemp asDblist4 = new ASM_FExpTemp();
				ASM_FExpTempDS DS5 = new ASM_FExpTempDS(getActivity());

				
				asDblist4.setAsmExpTEmp_RefNo(txtrefno.getText().toString());
				asDblist4.setAsmExpTEmp_ExpName(selectedReason.split("-:-")[0].toString());
				asDblist4.setAsmExpTEmp_ExpCode(selectedReason.split("-:-")[1].toString());
				asDblist4.setAsmExpTEmp_amunt(txtamt.getText().toString());
				
				DS5.Updatetemp(asDblist4);
				
				} catch (Exception e) 
				{
					Toast.makeText(getActivity(), "Record Update Not Sucessful...Try Again..", 
                         	   Toast.LENGTH_LONG).show();
					// TODO: handle exception
				}
	
	//*********Refresh newly edited UI**************//
					final Button tv0 = new Button(getActivity());
			        tv0.setText(selectedReason+"-:-"+txtamt.getText().toString());
			        tv0.setClickable(true);
			        tv0.setWidth(600);
			        tv0.setTextSize(19);
			        tv0.setTextColor(Color.BLUE);
			        tv0.setGravity(Gravity.LEFT);
			        tbrow0.addView(tv0);
			        stk.addView(tbrow0);
			        btnadd.setText("ADD");
			        spinner.setClickable(true);
			      //  txtamt.setText("");
			        
	//************Update edited Record**************//		        
			   	 tv0.setOnClickListener(new View.OnClickListener() 
			        {
						
						@Override
						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
							if(btnadd.getText().toString().equals("UPDATE"))
							{
								tv0.setClickable(false);
							}else 
							{
			
							spinner.clearAnimation();
							tbrow0.removeViews(0, 1);
							
							
							
							ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
							ArrayList<String> list =ds2.getExpTemp1(tv0.getText().toString().split("-:-")[1]);
							
							ASM_fExpenseDS ds3=new ASM_fExpenseDS(getActivity());
							ArrayList<String> list1 =ds3.getExpense();
							


							
							list1.add(0,list.get(0).split("-:-")[1].toString()+"-:-"+list.get(0).split("-:-")[2].toString());
							
							ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
							spinner.setAdapter(categoriesAdapter);
							
							txtamt.setText(list.get(0).split("-:-")[3].toString());
							tv0.setClickable(false);
							spinner.setClickable(false);
							btnadd.setText("UPDATE");
							}
						}
					});
//***********Delete Udated record************************//			   	 
				 tv0.setOnLongClickListener(new View.OnLongClickListener() 
				 {
					 
					
					@Override
					public boolean onLongClick(View v) 
					{
						tbrow0.removeViews(1, 0);
						// TODO Auto-generated method stub
						AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
						builder1.setTitle("ATTENTION..!");
						builder1.setIcon(R.drawable.ic_attention);
			            builder1.setMessage("Are You Sure Want to Delete this Record...?");
			            builder1.setCancelable(true);
			            builder1.setPositiveButton("YES",
			                new DialogInterface.OnClickListener()
			            {
			                public void onClick(DialogInterface dg, int id) 
			                {		
			                	tbrow0.removeViews(0,1);
								spinner.clearAnimation();
			                	   ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());

			                	   ds2.Delete(tv0.getText().toString().split("-:-")[1].trim());
			                	   
			           
			                }
			            });
			            builder1.setNegativeButton("NO", new DialogInterface.OnClickListener() 
			            {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								dialog.dismiss();
							}
						});
			            AlertDialog alert11 = builder1.create();
			            alert11.show();	
						return false;
					}
				});
				//}
					
				}
//*******************Add new expences*********************************//				
				else if (btnadd.getText().toString().equals("ADD")) 
				{
					if(!selectedReason.toString().equals("SELECT EXPENCES") && !txtamt.getText().toString().equals(""))
					{
						ASM_FExpTempDS ds3=new ASM_FExpTempDS(getActivity());
						ArrayList<String> list1 =ds3.getExpTemp1(selectedReason.split("-:-")[1].toString());

						
						if(!list1.toString().equals("[]") && list1.get(0).toString().split("-:-")[2].equals(selectedReason.split("-:-")[1].toString()))
						{

							Error1();

						}
						else 
						{
						
						
				       
				        
				        try 
				        {
				        final Button tv0 = new Button(getActivity());
				        tv0.setText(selectedReason+"-:-"+txtamt.getText().toString());
				        tv0.setClickable(true);
				        tv0.setWidth(600);
				        tv0.setTextSize(19);
				        tv0.setTextColor(Color.BLUE);
				        tv0.setGravity(Gravity.LEFT);
				        tbrow0.addView(tv0);
				        stk.addView(tbrow0);
				        
							
					
				        
				    	ASM_FExpTemp asDblist4 = new ASM_FExpTemp();
						ASM_FExpTempDS DS5 = new ASM_FExpTempDS(getActivity());
						
						
						asDblist4.setAsmExpTEmp_RefNo(txtrefno.getText().toString());
						//asDblist4.setAsmExpTEmp_(txtdate.getText().toString());
						asDblist4.setAsmExpTEmp_ExpName(selectedReason.split("-:-")[0].toString());
						asDblist4.setAsmExpTEmp_ExpCode(selectedReason.split("-:-")[1].toString());
						asDblist4.setAsmExpTEmp_amunt(txtamt.getText().toString());
						

						
						DS5.Create(asDblist4);

//*******************Update New Added expences*********************************//						
						 tv0.setOnClickListener(new View.OnClickListener() 
					        {
								
								@Override
								public void onClick(View v) 
								{
									// TODO Auto-generated method stub
									if(btnadd.getText().toString().equals("UPDATE"))
									{
										tv0.setClickable(false);
									}
									else 
									{
					
									tbrow0.removeViews(0,1);
									spinner.clearAnimation();
									getUpdateExpencesNo = tv0.getText().toString().trim();
									
									ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
									ArrayList<String> list =ds2.getExpTemp1(tv0.getText().toString().split("-:-")[1]);
									
									ASM_fExpenseDS ds3=new ASM_fExpenseDS(getActivity());
									ArrayList<String> list1 =ds3.getExpense();
									
									list1.add(0,list.get(0).split("-:-")[1].toString()+"-:-"+list.get(0).split("-:-")[2].toString());
									
									ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
									spinner.setAdapter(categoriesAdapter);
									spinner.setClickable(false);
									txtamt.setText(list.get(0).split("-:-")[3].toString());
									tv0.setClickable(false);
									btnadd.setText("UPDATE");
									}
									
								}
							});

//*******************Delete newly added expences*********************************//						 
						 tv0.setOnLongClickListener(new View.OnLongClickListener() 
						 {
							
							@Override
							public boolean onLongClick(View v) 
							{
								
								// TODO Auto-generated method stub
								AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
								builder1.setTitle("ATTENTION..!");
								builder1.setIcon(R.drawable.ic_attention);
					            builder1.setMessage("Are You Sure Want to Delete this Record...?");
					            builder1.setCancelable(true);
					            builder1.setPositiveButton("YES",
					                new DialogInterface.OnClickListener()
					            {
					                public void onClick(DialogInterface dg, int id) 
					                {
					                	tbrow0.removeViews(0,1);
										spinner.clearAnimation();
					                	   ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
					                	   ds2.Delete(tv0.getText().toString().split("-:-")[1].trim());
					                	   
					           
					                }
					            });
					            builder1.setNegativeButton("NO", new DialogInterface.OnClickListener() 
					            {
									
									@Override
									public void onClick(DialogInterface dialog, int which) {
										// TODO Auto-generated method stub
										dialog.dismiss();
									}
								});
					            AlertDialog alert11 = builder1.create();
					            alert11.show();	
								return false;
							}
						});
		
						
				    	} catch (Exception e) 
				    	{
				    		Toast.makeText(getActivity(), "Record Save Not Sucessful...Try Again..", 
	                         	   Toast.LENGTH_LONG).show();
							// TODO: handle exception
						}

					}
					}
					else
					{
						Error();

					}
				}
				else 
				{
					
				}
				
				
				
				
				
			}
		});
		
		btnsave.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
				ASM_FExpTempDS ds7=new ASM_FExpTempDS(getActivity());
				ArrayList<String> list7 =ds7.getExpTemp();	
				if(!list7.toString().equals("[]"))
				{
				try 
				{
	
				ASM_FDayExpHed asDblist6 = new ASM_FDayExpHed();
				ASM_FDayExpHedDS DS6 = new ASM_FDayExpHedDS(getActivity());
				
				ASM_FDayExpDet asDblist7 = new ASM_FDayExpDet();
				ASM_FDayExpDetDS DS7 = new ASM_FDayExpDetDS(getActivity());
				
				for (int i = 0; i < list7.size(); i++) 
				{
					
					
					asDblist7.setAsmDExpDet_RefNo(list7.get(i).split("-:-")[0].toString());
					asDblist7.setAsmDExpDet_ExpCode(list7.get(i).split("-:-")[1].toString());
					asDblist7.setAsmDExpDet_ExpName(list7.get(i).split("-:-")[2].toString());
					asDblist7.setAsmDExpDet_Amt(list7.get(i).split("-:-")[3].toString());
					DS7.Create(asDblist7);
					
					
				}
				
				asDblist6.setAsmDExpHed_RefNo(txtrefno.getText().toString());
				asDblist6.setAsmDExpHed_TxnDate(txtdate.getText().toString());
		
				if(txtremark.getText().toString().equals(""))
				{
					asDblist6.setAsmDExpHed_Remarks(" ");	
				}else
				{
					asDblist6.setAsmDExpHed_Remarks(txtremark.getText().toString());
				}
				
				asDblist6.setAsmDExpHed_AddDate(txtdate.getText().toString());
				asDblist6.setAsmDExpHed_AddUser("");
				asDblist6.setAsmDExpHed_Issynced("0");

				GPSTracker gps = new GPSTracker(getActivity());
				 if(gps.canGetLocation())
				 {
	                    
					 double lati = gps.getLatitude();
	                 double longi = gps.getLongitude();
	                 asDblist6.setAsmDExpHed_Longitide(Double.toString(longi));
	                 asDblist6.setAsmDExpHed_Latitude(Double.toString(lati));
	                 Toast.makeText(getActivity(), "Your Location is - \nLat: " + lati + "\nLong: " + longi, Toast.LENGTH_LONG).show();   

				 }
				 else
				 {
	                    gps.showSettingsAlert();
	                   double lati = gps.getLatitude();
	                   double longi = gps.getLongitude();
	                   asDblist6.setAsmDExpHed_Longitide(Double.toString(longi));
		               asDblist6.setAsmDExpHed_Latitude(Double.toString(lati));
	                   Toast.makeText(getActivity(), "Your Location is - \nLat: "  , Toast.LENGTH_LONG).show();   
	               }
				
				DS6.Create(asDblist6);
				
				
				ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
          	    ds2.Delete1(txtrefno.getText().toString().trim());
				
				
				
				} catch (Exception e) 
				{
					Toast.makeText(getActivity(), "Record Save Not Sucessful...Try Again..", 
                      	   Toast.LENGTH_LONG).show();
					// TODO: handle exception
				}
				
				addItem(txtrefno.getText().toString(), txtdate.getText().toString());
				String seperate = txtrefno.getText().toString().trim().substring(5);
				int incriment = Integer.parseInt(seperate);
				int newincriment = incriment + 1;
				ASM_FCompanyBranch asDblist1 = new ASM_FCompanyBranch();
				ASM_FCompanyBranchDS DS1 = new ASM_FCompanyBranchDS(getActivity());
				
				asDblist1.setAsmFCompanyBranch_cSettingsCode(cSettingcode);
				asDblist1.setAsmFCompanyBranch_nNumVal(Integer.toString(newincriment));
				DS1.Create(asDblist1);
				dialog.dismiss();
				
				}else
				{
					Error2();
				}
			}
		});
		
		btncancel.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				final ASM_FExpTempDS ds2=new ASM_FExpTempDS(getActivity());
				String chk = ds2.getEmptyOrnot(txtrefno.getText().toString());
				if(chk!=null)
				{
			
				// TODO Auto-generated method stub
				AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
				builder1.setTitle("ATTENTION..!");
				builder1.setIcon(R.drawable.ic_attention);
	            builder1.setMessage("Are You Sure Want to Exit without saving...?");
	            builder1.setCancelable(true);
	            builder1.setPositiveButton("YES",
	                new DialogInterface.OnClickListener()
	            {
	                public void onClick(DialogInterface dg, int id) 
	                {
	                	  
	                  	  ds2.Delete1(txtrefno.getText().toString());
	                	  dialog.dismiss();
	                	  //loadhistory();
	           
	                }
	            });
	            builder1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						//dg.dismiss();
						
	                	   dialog.dismiss();
	                	  // loadhistory();
	                
					}
				});
	            AlertDialog alert11 = builder1.create();
	            alert11.show();	
				//dialog.dismiss();
				}else {
					 dialog.dismiss();
					 //loadhistory();
				}
				
			}
		});
			 
		}
		 }
		 




	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{

		switch (item.getItemId()) 
		{
		case R.id.action_add_item:
			addwindow("ADDNEW","");
			
			return true;
				default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private String getCurrentTimeString() 
	{
		// 2013-10-04 00:00:00.000
		Date dateNow = new Date();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat ddMMMyyFormat = new SimpleDateFormat("dd/MM/yyyy");
		String date_to_string = ddMMMyyFormat.format(dateNow);
		String cusDate = date_to_string;
		return cusDate;
	}

	 
 
	 
	 public String refnogenrator(String cSettingsCode,String ASMPreFix) 
		{
			ASM_UsersettingsDS ds2=new ASM_UsersettingsDS(getActivity());
			ASMCode = ds2.getASMCode(); 
			
			ASM_fCompanySettingDS ds1=new ASM_fCompanySettingDS(getActivity());
			String refcode =ds1.getFcompanyDetails(cSettingsCode,ASMCode);
			
			ASM_FCompanyBranchDS ds=new ASM_FCompanyBranchDS(getActivity());
			String tempRepExpenseSerialStr = ds.getFcompanybranch(cSettingsCode);
			if(tempRepExpenseSerialStr==null)
			{
				tempRepExpenseSerialStr = "1";
			}
			
			String currentExpenseRefCode = null;
			
			

			try {
				if (tempRepExpenseSerialStr.length() == 0) 
				{
					String newInvNumberStr = "0000000" + tempRepExpenseSerialStr;
					currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
				} 
				else if (tempRepExpenseSerialStr.length() == 1)
				{
					String newInvNumberStr = "000000" + tempRepExpenseSerialStr;
					currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
				} 
				else if (tempRepExpenseSerialStr.length() == 2) 
				{
					String newInvNumberStr = "00000" + tempRepExpenseSerialStr;
					currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
				}
				else if (tempRepExpenseSerialStr.length() == 3) 
				{
					String newInvNumberStr = "0000" + tempRepExpenseSerialStr;
					currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
				} 
				else if (tempRepExpenseSerialStr.length() == 4) 
				{
					String newInvNumberStr = "000" + tempRepExpenseSerialStr;
					currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
				} 
				else if (tempRepExpenseSerialStr.length() == 5) 
				{
					String newInvNumberStr = "00" + tempRepExpenseSerialStr;
					currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
				} 
				else if (tempRepExpenseSerialStr.length() == 5) 
				{
					String newInvNumberStr = "0" + tempRepExpenseSerialStr;
					currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
				} 
				else 
				{
					String newInvNumberStr = tempRepExpenseSerialStr;
					currentExpenseRefCode = refcode.trim() + "/"+ newInvNumberStr;
				}
			} catch (Exception e) {

			}

			
			
			return currentExpenseRefCode;
		}
	 

	 
	 public void loadhistory() 
		{


			ASM_FDayExpHedDS ds2=new ASM_FDayExpHedDS(getActivity());
			ArrayList<String> list =ds2.getExpHedAll();
			ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);

			
			for (int i = 0; i < categoriesAdapter.getCount(); i++) 
			{
	
				addItem(list.get(i).split("-:-")[0].toString(),list.get(i).split("-:-")[1].toString());
				
			}

			
		}
	
	 public void Error() 
	 {
			AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
			builder1.setTitle("ERROR");
			builder1.setIcon(R.drawable.ic_error);
            builder1.setMessage("Please Enter Amount & Reason...");
            builder1.setCancelable(true);
            builder1.setPositiveButton("OK",
                new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dg, int id) 
                {
                	dg.dismiss();
           
                }
            });
            AlertDialog alert11 = builder1.create();
            alert11.show();	
	}
	 
	 public void Error1() 
	 {
			AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
			builder1.setTitle("ERROR");
			builder1.setIcon(R.drawable.ic_error);
            builder1.setMessage("This Expences Already added...");
            builder1.setCancelable(true);
            builder1.setPositiveButton("OK",
                new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dg, int id) 
                {
                	dg.dismiss();
           
                }
            });
            AlertDialog alert11 = builder1.create();
            alert11.show();	
	}
	 
	 public void Error2() 
	 {
			AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
			builder1.setTitle("ERROR");
			builder1.setIcon(R.drawable.ic_error);
            builder1.setMessage("No Added Item For the Save...");
            builder1.setCancelable(true);
            builder1.setPositiveButton("OK",
                new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dg, int id) 
                {
                	dg.dismiss();
           
                }
            });
            AlertDialog alert11 = builder1.create();
            alert11.show();	
	}
	 
	
	 
	   private void addItem(String txtrefno,String getSelectedReson) 
	    {
	        // Instantiate a new "row" view.
	        final ViewGroup newView = (ViewGroup) LayoutInflater.from(getActivity()).inflate(
	                R.layout.list_item_example, mContainerView, false);
	        ((TextView) newView.findViewById(android.R.id.text1)).setText("");
	        // Set the text in the new row to a random country.
	       ((TextView) newView.findViewById(android.R.id.text1)).setText(txtrefno.toString()+" -:- "+getSelectedReson.toString());

	        // Set a click listener for the "X" button in the row that will remove the row.
	        newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() 
	        {
	            @Override
	            public void onClick(View view) 
	            {
	        		AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
	        		builder1.setTitle("ATTENTION..!");
	    			builder1.setIcon(R.drawable.ic_attention);
		            builder1.setMessage("Are You Sure Want to Delete This Record..? This Step Can't Be Reverse..");
		            builder1.setCancelable(true);
		            builder1.setPositiveButton("OK",
		            new DialogInterface.OnClickListener()
		            {
		                public void onClick(DialogInterface dg, int id) 
		                {
		                	   mContainerView.removeView(newView);
		                	   String refno  = ((TextView) newView.findViewById(android.R.id.text1)).getText().toString().substring(0, 12);
		                	   ASM_FDayExpDetDS ds2=new ASM_FDayExpDetDS(getActivity());
		                	   ASM_FDayExpHedDS ds3=new ASM_FDayExpHedDS(getActivity());
		                	   ds2.Delete(refno);
		                	   ds3.Delete(refno);

		                       if (mContainerView.getChildCount() == 0) 
		                       {
		                           
		                       }
		                	dg.dismiss();
		           
		                }
		            });
		            
		            builder1.setNegativeButton("NO",  
		            new DialogInterface.OnClickListener()
		            {
		                public void onClick(DialogInterface dg, int id) 
		                {
		                dg.dismiss();
		                }
		            });
		            AlertDialog alert11 = builder1.create();
		            alert11.show();
	                // Remove the row from its parent (the container view).
	                // Because mContainerView has android:animateLayoutChanges set to true,
	                // this removal is automatically animated.
	             
	            }
	        });
	        newView.findViewById(R.id.add_buttton).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) 
				{
					
					String refno  = ((TextView) newView.findViewById(android.R.id.text1)).getText().toString().substring(0, 12);
					
					
					addwindow("UPDAET",refno);
					mContainerView.removeAllViews();
					// TODO Auto-generated method stub
			
					
				}
			});
	        
	        newView.findViewById(R.id.view_button).setOnClickListener(new View.OnClickListener() {
				
	    			@Override
	    			public void onClick(View v) 
	    			{

	    				
	    				
	    				String refno  = ((TextView) newView.findViewById(android.R.id.text1)).getText().toString().substring(0, 12);
	    				ViewDetails(refno);
	    				// TODO Auto-generated method stub
	    			
	    			}
	    		});
	        

	        mContainerView.addView(newView, 0);
	    }
	   
	   public void ViewDetails(String RefNo) 
		{
			
			 final Dialog dialog = new Dialog(getActivity());
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(R.layout.asm_dialogexpencesview);
			dialog.setCancelable(false);
			dialog.show();
			
			TextView txtRefno = (TextView)dialog.findViewById(R.id.txtRefno);
			TextView txtDate = (TextView)dialog.findViewById(R.id.txtDate);
			TextView txtRemk = (TextView)dialog.findViewById(R.id.txtRemk);

			
			ASM_FDayExpHedDS ds3=new ASM_FDayExpHedDS(getActivity());
			ArrayList<String> list1 =ds3.getfDayExpHed(RefNo);
			
			
			txtRefno.setText(list1.get(0).split("~")[0].toString());
			txtDate.setText(list1.get(0).split("~")[1].toString());
			txtRemk.setText(list1.get(0).split("~")[2].toString());

			TableLayout stk = (TableLayout)dialog.findViewById(R.id.table_main);
	        TableRow tbrow0 = new TableRow(getActivity());
	        tbrow0.setGravity(Gravity.RIGHT);
	        
       
	        TextView tv0 = new TextView(getActivity());
	        tv0.setText(" EXPENCES NAME ");
	        tv0.setClickable(false);
	        
	        tv0.setTextSize(18);
	        tv0.setTextColor(Color.BLUE);
	        tv0.setGravity(Gravity.LEFT);
	        tbrow0.addView(tv0);
	        
	        TextView tv2 = new TextView(getActivity());
	        tv2.setText(" AMOUNT ");
	        tv2.setClickable(false);
	        tv2.setTextSize(18);
	        tv2.setGravity(Gravity.LEFT);
	        tv2.setTextColor(Color.BLUE);
	        tbrow0.addView(tv2);
	        
	      

	        stk.addView(tbrow0);
	        
	        
	        ASM_FDayExpDetDS ds4=new ASM_FDayExpDetDS(getActivity());
			ArrayList<String> list2 =ds4.getfDayExpDet(RefNo);

			
			for (int i = 0; i <list2.size(); i++) 
			{

			  TableRow tbrow = new TableRow(getActivity());
			  TextView t1v = new TextView(getActivity());     
		      t1v.setText(" "+list2.get(i).split("~")[0].toString());
		      t1v.setTextColor(Color.WHITE);
		      t1v.setTextSize(17);
		      t1v.setGravity(Gravity.LEFT);
		      t1v.setClickable(true);
		      tbrow.addView(t1v);
				
		      TextView t2v = new TextView(getActivity());
			  t2v.setTextColor(Color.WHITE);
	          t2v.setText("Rs "+list2.get(i).split("~")[1].toString()+"/=");
	          t2v.setTextSize(17);
	          t2v.setGravity(Gravity.CENTER);
	          t2v.setClickable(true);
	          tbrow.addView(t2v);
	          
	         
	          stk.addView(tbrow);

			}
			Button btnOK = (Button)dialog.findViewById(R.id.btnOK);
			btnOK.setOnClickListener(new View.OnClickListener()
			{
				
				@Override
				public void onClick(View v) 
				{
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
		}


	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	     if(keyCode==KeyEvent.KEYCODE_BACK)
	         Toast.makeText(getActivity(), "Use Home Button",      
	      Toast.LENGTH_LONG).show();

	     return false;
	        // Disable back button..............
	 }
}
